layui.use(['table','layer','form','laytpl'], function(){
	var table = layui.table,
	layer = layui.layer,
	$ = layui.$,
	form = layui.form;
	var laytpl = layui.laytpl;
	var result_arr = [];//选中维修配件接口返回集合
	var stock_id = [];//选中的stock_id集合
	var del_id = [];//删除的stock_id集合
	console.log(222222)
	/*@初始化所有维修配件table*/
	table.render({
	    elem: '#all-goods',
	    id: 'change_outward',
	    url: '/repair/goods/allGoods/ty/edit', //数据接口
	    page: true, //开启分页
	    cols: [[ //表头
	    	{field: 'goods_id', title: '序号'},
	    	{field: 'goods_sn', title: '产品编码'},
	    	{field: 'goods_name', title: '产品名称'},
	    	{field: 'category_name', title: '产品分类'},
	    	{field: 'cate_name', title: '产品子分类'},
	    	{field: 'brand_name', title: '品牌'},
	    	{field: 'marker', title: '型号'},
	    	{field: 'attribute_name', title: '属性'},
	    	{field: 'colour_name', title: '颜色'},
	    	{field: 'number', title: '剩余数量'},
	    	{field: 'unit', title: '单位'},
	    	{field: 'selling_price', title: '零售单价(元)'},
	    	{field: 'trade_price', title: '批发价(元)'},
	    	{field: 'buying_price', title: '进货价(元)'},
	    	{title: '操作', width: 100, align:'center', toolbar: '#all-oper'},
	    ]]
	});
	/*@初始化已选择维修配件table*/
	table.render({
	    elem: '#repair-goods',
	    id: 'repair_goods',
	    url: '/repair/goods/repair_goods/ty/edit', //数据接口
	    page: false, //开启分页
	    limit: 'limit',
	    cols: [[ //表头
	    	{field: 'goods_id', title: '序号'},
	    	{field: 'goods_sn', title: '产品编码'},
	    	{field: 'goods_name', title: '产品名称'},
	    	{field: 'category_name', title: '产品分类'},
	    	{field: 'cate_name', title: '产品子分类'},
	    	{field: 'brand_name', title: '品牌'},
	    	{field: 'marker', title: '型号'},
	    	{field: 'attribute_name', title: '属性'},
	    	{field: 'colour_name', title: '颜色'},
	    	{field: 'number', title: '剩余数量'},
	    	{field: 'unit', title: '单位'},
	    	{field: 'selling_price', title: '零售单价(元)'},
	    	{field: 'trade_price', title: '批发价(元)'},
	    	{field: 'buying_price', title: '进货价(元)'},
	    	{title: '操作', width: 100, align:'center', toolbar: '#change-oper'},
	    ]],
	    /*渲染后回调*/
	    done: function(res){
	    },
	    /*渲染前，请求成功后回调*/
	    parseData: function(res){
	    	// console.log(res)
	    	/*@修改已选维修配件数据*/
			for(var i = 0;i < parent.layui.result_arr.length;++i){
				res.count += 1
				res.data[res.count-1] = parent.layui.result_arr[i]
			}
	    }
	});
  	/*@表格tr双击事件*/
  	table.on('rowDouble(list)', function(obj){
  		/*本页面table*/
  		table.reload(
  			'repair_goods',
  			{
  				parseData: function(res){
  					var ind_1 = $.inArray( obj.data.stock_id, parent.layui.stock_id)
  					var ind_2 = $.inArray( obj.data.stock_id, parent.layui.have_id)
  					var ind_3 = $.inArray( obj.data.stock_id, parent.layui.del_id)
  					if( ind_1 == -1 ){/*不存在*/
  						if(ind_2 == -1 || (ind_2 >= 0 && ind_3 >= 0 ) ){
  							if(ind_2 == -1){
  								obj.data.num = 1
  							}
							parent.layui.stock_id.push(obj.data.stock_id)
							parent.layui.result_arr.push(obj.data)
							if(ind_3 >= 0){
								parent.layui.del_id.splice(ind_3,1);
							}
  						}
					}
					/*@修改已选维修配件数据*/
					for(var i = 0;i < parent.layui.result_arr.length;++i){
						res.count += 1
						res.data[res.count-1] = parent.layui.result_arr[i]
					}
					/*父页面table*/
  					father_table()
  				}
  			}
  		);
	});
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	/*@事件监听*/
	  	if(layEvent === 'purchase'){ //缺货采购
	  		layui.purchase = data
	  		xadmin.open('缺货采购','/repair/goods/purchase')
	    	// layer.msg('缺货采购')
	  	} else if(layEvent === 'cencel'){ //取消
	  		layer.confirm('确定取消', {icon: 3,title: '提示'}, function(index){
	  			
	  			/*@数据处理*/
	  			var ind_1 = $.inArray( data.stock_id, parent.layui.stock_id)
	  			var ind_2 = $.inArray( data.stock_id, parent.layui.have_id)
	  			var ind_3 = $.inArray( data.stock_id, parent.layui.del_id)
	  			if( Number(ind_1) >= 0 || Number(ind_2) >= 0){/*存在*/
	  				if(ind_1 >= 0){
	  					parent.layui.stock_id.splice(ind_1,1);
	  				}
	  				if(ind_2 >= 0 && ind_3 == -1){
	  					parent.layui.del_id.push(data.stock_id)
	  					// parent.layui.have_id.splice(ind_2,1);
	  				}
	  				for(var i = 0;i < parent.layui.result_arr.length;++i){
	  					if(parent.layui.result_arr[i].stock_id == data.stock_id){
	  						parent.layui.result_arr.splice(i,1);
	  					}
	  				}
	  			}
	  			/*@dome操作*/
	  			$(tr).remove()
	  			layer.close(index)
	  			/*父页面操作*/
	  			father_table()
	  			var price = data.selling_price * data.num
	  			parent.layui.cut_price(price)
	  			window.parent.repair_money_fun()
	  		})
	  	}
	});
	/*@父页面表单重载*/
	father_table = function(){
		parent.layui.table.reload(
  			'repair_goods',
  			{
  				parseData: function(res){
					/*@修改已选维修配件数据*/
					for(var i = 0;i < parent.layui.result_arr.length;++i){
						var ind_2 = $.inArray( parent.layui.result_arr[i].stock_id, parent.layui.have_id)
						if(ind_2 == -1){/*初始，未拥有*/
							res.count += 1
							res.data[res.count-1] = parent.layui.result_arr[i]
						}else{/*初始拥有，变成拥有*/
							parent.layui.result_arr[i] = res.data[ind_2]
						}
					}
					var all_price = 0
					for(var i = 0;i < res.data.length;++i){
						var ind_res = $.inArray( res.data[i].stock_id, parent.layui.del_id)
						if(ind_res >= 0){/*删除*/
							res.data.splice(ind_res,1);
						}else{/*未删除*/
							var price = Number(res.data[i].num)*Number(res.data[i].selling_price*100)/100
							all_price += price
						}
					}
					parent.layui.add_price(all_price)
					window.parent.repair_money_fun()
  				}
  			}
  		);
	}
})